package com.meituan.controller;

import com.github.pagehelper.PageInfo;
import com.meituan.mapper.ShopMapper;
import com.meituan.model.ShopEntity;
import com.meituan.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class ShopController {
    @Autowired
    ShopService shopService;
    @GetMapping("getShopPage")
    public Map getShopPage(String merchantId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        Map map = new HashMap();
        PageInfo<ShopEntity> pageInfo = shopService.findPageAllByMerchantId(merchantId,pageNum,pageSize);
        if(pageInfo.getList().size()!=0){
            map.put("err_code",0);
            map.put("result",pageInfo);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("getShop")
    public Map getShop(String merchantId){
        Map map = new HashMap();
        List<ShopEntity> list = shopService.findAllByMerchantId(merchantId);
        if(list.size()!=0){
            map.put("err_code",0);
            map.put("result",list);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("regShop")
    public Map regShop(@RequestBody ShopEntity shop){
        Map map = new HashMap();
        if(shopService.register(shop)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("delShop")
    public Map delShop(String shopId){
        Map map = new HashMap();
        Integer result = shopService.remove(shopId);
        if(result==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("updShop")
    public Map updShop(@RequestBody ShopEntity shop){
        Map map = new HashMap();
        Integer result = shopService.updateShop(shop);
        if(result==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("getShopWithGoods")
    public List<ShopEntity> getShopWithGoods(){
        Map map = new HashMap();
        List<ShopEntity> list = shopService.getAll();
        return list;
    }

    @GetMapping("getShopById")
    public ShopEntity getShopById(String shopId){
        Map map = new HashMap();
        ShopEntity shop = shopService.findById(shopId);
        return shop;
    }

    @PostMapping("uploadShop")
    public Map uploadShop(MultipartFile file, String shopId){
        Map map = new HashMap();
        String fileName = file.getOriginalFilename();
        String fileSuffix =fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        String finalName = format+'.'+fileSuffix;
        String imgPath = "/static/shopImg?filename="+finalName;
        shopService.updateShopImg(shopId,imgPath);
        String diskPath = "G:\\meituanUpload\\shopImg";
        File localFile = new File(diskPath,finalName);
        if(!localFile.getParentFile().exists()){
            localFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(localFile);
            map.put("err_code",0);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("err_code",1);
        }
        return map;
    }
}
